<?php

namespace App\Repos;

use App\Models\UserExpert;
use App\Models\User;

class UserExpertRepo 
{
    public static function pages($where=[])
    {	
    	$query = UserExpert::query();
    	$list = $query->where($where)->orderBy('weight','asc')->paginate(10);
    	return $list;
    }

    public static function find($where=[])
    {
    	return UserExpert::where($where)->first();
    } 

    public static function save($data)
    {
        
        $user = array_only($data,['username','truename','mobile','openid','nickname','avatar']);
        $salt = $string = str_random(40);
        $user['salt']     = $salt; 
        $user['password'] = md5($data['password'].$salt);
        $user['role_id']  = 1;
        $flag   = User::create($user);
        $expert = array_only($data,['good_at','rank','status','contents','point','weight']);
        $expert['user_id'] = $flag->id;
        UserExpert::create($expert);
        return true;
    }

    public static function update($where=[],$data)
    {
        

        if(array_get($data,'avatar')) {
            $user['avatar']  = array_pull($data,'avatar');
            $userWhere['id'] = $data['user_id']; 
            User::where($userWhere)->update($user);
        }       
        
        return UserExpert::where($where)->update($data);
    }
}
